package com.un.ebs.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.un.ebs.core.enums.PageTypeEnum;
import com.un.ebs.core.exception.BusinessEntityNotFoundException;
import com.un.ebs.core.util.SecurityUtils;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.sys.domain.Page;
import com.un.ebs.sys.dto.PageAddDto;
import com.un.ebs.sys.dto.PageUpdateDto;
import com.un.ebs.sys.mapper.PageMapper;
import com.un.ebs.sys.service.PageService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liubo
 * @since 2020-04-19
 */
@Service
public class PageServiceImpl extends ServiceImpl<PageMapper, Page> implements PageService {

    @Override
    public void add(PageAddDto req) {
        Page obj = new Page();
        obj.setClassId(req.getClassId());
        obj.setPageType(req.getPageType());
        obj.setTitle(req.getTitle());
        obj.setRemark(req.getRemark());
        obj.setConfigJson(req.getConfigJson());
        obj.setRunJson(req.getRunJson());
        obj.setRenderEngineVer(req.getRenderEngineVer());
        obj.setAllowAnonymous(req.getAllowAnonymous());
        obj.insert();
    }

    @Override
    public void update(PageUpdateDto req) {
        Page obj = getById(req.getId());
        obj.setClassId(req.getClassId());
        obj.setPageType(req.getPageType());
        obj.setTitle(req.getTitle());
        obj.setRemark(req.getRemark());
        obj.setRunJson(req.getRunJson());
        obj.setConfigJson(req.getConfigJson());
        obj.setRenderEngineVer(req.getRenderEngineVer());
        obj.setAllowAnonymous(req.getAllowAnonymous());
        obj.updateById();
    }

    @Override
    public void delete(String id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void copyTo(String sourceId, String newName) {
        Page page = getById(sourceId);
        if (page == null) {
            throw new BusinessEntityNotFoundException(sourceId, "页面");
        }

        PageAddDto obj = new PageAddDto();
        obj.setTitle(newName);
        obj.setClassId(page.getClassId());
        obj.setConfigJson(page.getConfigJson());
        obj.setRunJson(page.getRunJson());
        obj.setPageType(page.getPageType());
        obj.setRemark(page.getRemark());
        obj.setRenderEngineVer(page.getRenderEngineVer());
        obj.setAllowAnonymous(page.getAllowAnonymous());
        add(obj);
    }

    @Override
    public void moveTo(String id, String classId) {
        Page page = getById(id);
        if (page == null) {
            throw new BusinessEntityNotFoundException(id, "页面");
        }

        page.setClassId(classId);
        page.updateById();
    }

    @Override
    public Page queryById(String id) {
        return getBaseMapper().selectById(id);
    }

    @Override
    public IPage<Page> query(String title, String classId, PageTypeEnum type, int pageIndex, int pageSize) {
        IPage<Page> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageIndex, pageSize);
        LambdaQueryWrapper<Page> lq = Wrappers.lambdaQuery();
        lq.eq(Page::getCreateAt, SecurityUtils.getUserContent().getId());
        if (!StringUtils.isNullOrEmpty(title)) {
            lq.like(Page::getTitle, title);
        }
        if (!StringUtils.isNullOrEmpty(classId)) {
            lq.eq(Page::getClassId, classId);
        }
        if (type != null) {
            lq.eq(Page::getPageType, type.getValue());
        }
        lq.orderByDesc(Page::getCreateTime);
        return baseMapper.selectPage(page, lq);
    }
}